<template>
	<view>
		<z-paging ref="paging" v-model="productList" @query="productData">

			<view class="searchStyle">
				<uni-easyinput prefixIcon="search" v-model="value" placeholder="请输入药品名称" @confirm="searchList">
				</uni-easyinput>
			</view>

			<view v-if=" productList.length > 0 ">
				<scroll-view scroll-y="true" style="height: 1200rpx;" enable-back-to-top="true"
					bindscrolltolower="loadMore">
					<block v-for="(item,index) in productList" :key="index">
						<view class="p30 pl40 pr40 bb1 flex_lr_m">
							<view @click="onGotoDetail" class="">
								<view class="c333 f32">{{item.name}}</view>
								<view class="c333 f28 pl20">{{item.spec}}</view>
							</view>
							<view class="b color-primary" @click="onAddDrug(item)">+添加</view>
						</view>

					</block>
				</scroll-view>
			</view>
		</z-paging>
	</view>
</template>

<script>
	import {
		qzApi
	} from '../../config/request'
	export default {
		data() {
			return {
				productList: [],
				testMap: {
					code: 0,
					data: [],
					msg: "成功",
					timestamp: ""
				},
				queryForm: {
					pageNum: 1,
					pageSize: 10,
					name: '',
					type: [2], // 1:无需处方,2:支持线上处方,3:强制纸质处方
					displayMode: '0' // 0:其他，1首页,
				},
				loadCompleted: false,
				value: ''
			}
		},
		methods: {
			onChange(e) {
				this.queryForm.name = e.detail

				if (e.detail.length == 0) {
					this.queryForm.pageNum = 1
					this.loadCompleted = false
				}
			},

			async productData(pageNo, pageSize) {
				if(this.value){
					let form = {
						pageNum: pageNo,
						pageSize: pageSize,
						name: this.value,
						type: [2], // 1:无需处方,2:支持线上处方,3:强制纸质处方
						displayMode: '0' // 0:其他，1首页,
					}
					
					try {
						const data = await qzApi.getDrugList(form);
						this.$refs.paging.complete(data.data);
						// productList.value = data.data
					} catch (error) {}
					
				}else{
					this.$refs.paging.complete([]);
				}
				
				

			},
			searchList() {
				this.productData(1, 10)
			},
			onSearch() {
				console.log('onSearch');
				this.queryForm.pageNum = 1
				this.loadCompleted = false
				this.getRecommendDrugListRequest(this.queryForm)
			},
			onGotoDetail(e) {
				console.log(e)
				const drugId = e.currentTarget.dataset.id;
				// console.log(drugId);
				// 跳转到商品详情页面
				uni.navigateTo({
					url: `/pages/drugDetail/drugDetail?drugId=${drugId}`
				});
			},
			onAddDrug(e) {
				console.log(e);
				let item = e;
				item.quantity = 1
				var pages = getCurrentPages()
				var prevPage = pages[pages.length - 2]
				let arr = prevPage.drugArr
				if (Array.isArray(arr)) {
				  let flag = false
				  arr.forEach(element => {
				    if (element.id == item.id) {
				      element.quantity += item.quantity
				      flag = true
				    }
				  });
				  if (!flag) {
				    arr.push(item)
				  }
				} else {
				  arr = [item]
				}
				console.log(arr)
				prevPage.drugArr=arr
			
				uni.navigateBack({
				  delta: 1
				})
			},
			// 获取药品列表请求
			async getRecommendDrugListRequest(params) {
				try {
					uni.showLoading()
					await request.getDrugList(params).then((res) => {
						if (Array.isArray(res.data)) {
							if (res.data.length < 10) {
								this.loadCompleted = true
							}
							if (thi == 1) {
								thi = []
							}
							this.productList = this.productList.concat(res.data)
							this.productList = this.productList;

						}
					}).catch((err) => {
						uni.showToast({
							title: err,
							icon: 'none',
							duration: 2000
						});
					})
				} catch (error) {} finally {
					uni.hideLoading()
				}
			},
			gotoConsultationInfo() {
				uni.navigateTo({
					url: '/page1/consultationInfo/consultationInfo',
				})
			},
			loadMore() {
				console.log('loadMore');
				if (!this.data.loadCompleted) {
					this.setData({
						'query.pageNum': this.data.queryForm.pageNum += 1
					})
					this.getRecommendDrugListRequest(this.data.queryForm)
				}
			},
		}
	}
</script>

<style scoped>
	page {
		background: #fff;
	}

	.searchStyle {
		padding: 20upx;
	}
</style>